/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.command.factory;

import java.io.Serializable;
import java.util.List;

import org.openinsula.vulcano.core.command.Command;
import org.openinsula.vulcano.core.command.invoker.CommandInvoker;
import org.openinsula.vulcano.orm.InitializingCallback;
import org.openinsula.vulcano.orm.command.CommandOperations;
import org.openinsula.vulcano.orm.command.CommandTemplate;
import org.openinsula.vulcano.orm.command.InitializingCommandWrapper;
import org.openinsula.vulcano.orm.command.IterableInitializingCommandWrapper;
import org.openinsula.vulcano.orm.dao.Dao;
import org.openinsula.vulcano.orm.dao.query.DaoQuery;

/**
 * Factory that create commonly used {@link Command} implementation that mimic
 * {@link Dao} methods.
 * 
 * @author rebola
 * @author yanaga
 * @since 1.1
 * @see Command
 * @see CommandInvoker
 * @see CommandOperations
 * @see CommandTemplate
 * @see InitializingCommandWrapper
 * @see IterableInitializingCommandWrapper
 */
public abstract class CommandFactory {

	public static <T> Command<T> newFindById(final Class<T> entityClass, final Serializable id) {
		return new FindByIdCommand<T>(entityClass, id);
	}

	public static <T> Command<T> newFindById(final Class<T> entityClass, final Serializable id, final InitializingCallback<T> callback) {
		return new InitializingCommandWrapper<T>(newFindById(entityClass, id), callback);
	}

	public static <T> Command<T> newFindUnique(final DaoQuery<T> daoQuery) {
		return new FindUniqueCommand<T>(daoQuery);
	}

	public static <T> Command<T> newFindUnique(final DaoQuery<T> daoQuery, final InitializingCallback<T> callback) {
		return new InitializingCommandWrapper<T>(CommandFactory.<T> newFindUnique(daoQuery), callback);
	}

	public static <T> Command<List<T>> newFind(final DaoQuery<T> daoQuery) {
		return new FindCommand<T>(daoQuery);
	}

	public static <T> Command<List<T>> newFind(final DaoQuery<T> daoQuery, final InitializingCallback<T> callback) {
		return new IterableInitializingCommandWrapper<T>(CommandFactory.<T> newFind(daoQuery), callback);
	}

	public static Command<Object> newPersist(final Object... entities) {
		return new PersistCommand(entities);
	}

	public static Command<Object> newRemove(final Object... entities) {
		return new RemoveCommand(entities);
	}

	public static Command<Object> newRefresh(final Object... entities) {
		return new RefreshCommand(entities);
	}

}